BuildingHelper = {}
BuildingHelper.__index = BuildingHelper
 
function BuildingHelper.new()
    local instance = {

    }
    
    setmetatable(instance, BuildingHelper)
    return instance
end

function BuildingHelper:propOverLay(x, y)    
    local iX, iY = MainLayer:wndToWorld(x, y)
    local props = {gamePartition:propListForPoint(iX, iY, 0, MOAILayer.SORT_PRIORITY_DESCENDING)}
    local isClicked = false
    for k, v in pairs(props) do
      if v == propOverlay then
        isClicked = true
        break
      end
    end
    
    if isClicked == true then
      local widthOverlayX1 = (baseScreenWidth / 2) - 100
      local widthOverlayX2 = (baseScreenWidth / 2) + 100
      
      buildingObject = clickedProductionBuilding[1]
      local buildingProp = clickedProductionBuilding[2]
      local tileProduced = buildingObject:getTileWhichPlacedOn()

      if x > ((widthOverlayX1 + widthOverlayX2) / 2) + 18 then
        if buildingObject:getTypeBarracks() == "Cavalryy" then
          -- light horse
          if currentPlayerTurn:getGold() >= 150 then
            local lightHorse = Light_Horse.new()
            
            lightHorse:setOwnerObject(currentPlayerTurn)
            lightHorse:setRectangleValues(buildingProp:getRect())
            lightHorse:spawnUnit(buildingProp:getRect())
            lightHorse:setHasMoved(false)
            lightHorse:setTileUnitOn(tileProduced)
            currentPlayerTurn:addUnitToList(lightHorse)
            
            tileProduced:setUnit(lightHorse)

            isProductionOverlayShowed = false
            propOverlay:moveLoc(0,-80,1)
            if buildingSelectionProp ~= nil then
              MainLayer:removeProp(buildingSelectionProp)
              buildingSelectionProp = nil
            end
          else
            screenActionHelper:notEnoughGold()
          end
        elseif buildingObject:getTypeBarracks() == "Meleee" then
          -- Spearmen
          if currentPlayerTurn:getGold() >= 200 then
            local spearmen = Spearmen.new()
            
            spearmen:setOwnerObject(currentPlayerTurn)
            spearmen:setRectangleValues(buildingProp:getRect())
            spearmen:spawnUnit(buildingProp:getRect())
            spearmen:setHasMoved(false)
            currentPlayerTurn:addUnitToList(spearmen)
            spearmen:setTileUnitOn(tileProduced)
            tileProduced:setUnit(spearmen)

            isProductionOverlayShowed = false
            propOverlay:moveLoc(0,-80,1)
            if buildingSelectionProp ~= nil then
              MainLayer:removeProp(buildingSelectionProp)
              buildingSelectionProp = nil
            end
          else
            screenActionHelper:notEnoughGold()
          end
        elseif buildingObject:getTypeBarracks() == "Rangee" then
          -- spearthrower
          if currentPlayerTurn:getGold() >= 150 then
            local spearthrower = SpearThrower.new()
            
            spearthrower:setOwnerObject(currentPlayerTurn)
            spearthrower:setRectangleValues(buildingProp:getRect())
            spearthrower:spawnUnit(buildingProp:getRect())
            spearthrower:setHasMoved(false)
            currentPlayerTurn:addUnitToList(spearthrower)
            spearthrower:setTileUnitOn(tileProduced)
            tileProduced:setUnit(spearthrower)

            isProductionOverlayShowed = false
            propOverlay:moveLoc(0,-80,1)
            if buildingSelectionProp ~= nil then
              MainLayer:removeProp(buildingSelectionProp)
              buildingSelectionProp = nil
            end
          else
            screenActionHelper:notEnoughGold()
          end
        end
      end
      
      if isClicked == true then
        -- x < ((widthOverlayX1 + widthOverlayX2) / 2) + 18
        -- wordt niet gebruikt omdat het anders bij hogere resoluties de verkeerde units spawned.
        if buildingObject:getTypeBarracks() == "Cavalry" then
          -- heavy horse
          if currentPlayerTurn:getGold() >= 350 then  
            self:tutorial()
            local heavyhorse = Heavy_Horse.new()
            
            heavyhorse:setOwnerObject(currentPlayerTurn)
            heavyhorse:setRectangleValues(buildingProp:getRect())
            heavyhorse:spawnUnit(buildingProp:getRect())
            heavyhorse:setHasMoved(false)
            currentPlayerTurn:addUnitToList(heavyhorse)
            heavyhorse:setTileUnitOn(tileProduced)
            tileProduced:setUnit(heavyhorse)
   
            isProductionOverlayShowed = false
            propOverlay:moveLoc(0,-80,1)
            if buildingSelectionProp ~= nil then
              MainLayer:removeProp(buildingSelectionProp)
              buildingSelectionProp = nil
            end
          else
            screenActionHelper:notEnoughGold()
          end
        elseif buildingObject:getTypeBarracks() == "Melee" then
          -- swordfigher
          if currentPlayerTurn:getGold() >= 150 then
            self:tutorial()
            local sword = SwordFighter.new()
            
            sword:setOwnerObject(currentPlayerTurn)
            sword:setRectangleValues(buildingProp:getRect())
            sword:spawnUnit(buildingProp:getRect())
            sword:setHasMoved(false)
            currentPlayerTurn:addUnitToList(sword)
            sword:setTileUnitOn(tileProduced)
            tileProduced:setUnit(sword)
            
            isProductionOverlayShowed = false
            propOverlay:moveLoc(0,-80,1)
            if buildingSelectionProp ~= nil then
              MainLayer:removeProp(buildingSelectionProp)
              buildingSelectionProp = nil
            end
          else
            screenActionHelper:notEnoughGold()
          end
        elseif buildingObject:getTypeBarracks() == "Range" then
          -- archer
          if currentPlayerTurn:getGold() >= 100 then
            self:tutorial()
            local archer = Archer.new()
            
            archer:setOwnerObject(currentPlayerTurn)
            archer:setRectangleValues(buildingProp:getRect())
            archer:spawnUnit(buildingProp:getRect())
            archer:setHasMoved(false)
            currentPlayerTurn:addUnitToList(archer)
            archer:setTileUnitOn(tileProduced)
            tileProduced:setUnit(archer)          
            
            isProductionOverlayShowed = false
            propOverlay:moveLoc(0,-80,1)
            if buildingSelectionProp ~= nil then
              MainLayer:removeProp(buildingSelectionProp)
              buildingSelectionProp = nil
            end
          else
            screenActionHelper:notEnoughGold()
          end
        end          
      end
    elseif propOverlay ~= nil then
      -- er wordt buiten de propoverlay geklikt en moet weggehaald worden.
      isProductionOverlayShowed = false
      propOverlay:moveLoc(0,-80,1)
    end
end

function BuildingHelper:tutorial()
  if currentPlayerTurn == pl1 and tutTapIcon1 == true then MainLayer:removeProp(tutRed2) end
    if currentPlayerTurn == pl2 and tutTapIcon2 == true then MainLayer:removeProp(tutBlue2) end
    
    if currentPlayerTurn == pl1 and tutTapMove1 == false and chosenLevel == 1 then
      tutTapMove1 = true
      local image = MOAIGfxQuad2D.new()
      image:setTexture("resources/graphics/ingame_menu/tutorialred3.png")
      image:setRect(200, -200, 500, -75)

      tutRed3 = MOAIProp2D.new()
      tutRed3:setDeck(image)
      tutRed3:setPriority(1000)
      MainLayer:insertProp(tutRed3)
    end
    
    if currentPlayerTurn == pl2 and tutTapMove2 == false and chosenLevel == 1 then
      tutTapMove2 = true
      local image = MOAIGfxQuad2D.new()
      image:setTexture("resources/graphics/ingame_menu/tutorialblue3.png")
      image:setRect(270, -630, 570, -500)
        
      tutBlue3 = MOAIProp2D.new()
      tutBlue3:setDeck(image)
      tutBlue3:setPriority(1000)
      MainLayer:insertProp(tutBlue3)
    end
end

function BuildingHelper:buildingProps(tile, b, tileProp, buildingsList)
  if buildingSelectionProp ~= nil then
    MainLayer:removeProp(buildingSelectionProp)
    buildingSelectionProp = nil
  end
    
  if tile:getCanUnitBePlaced() == true and tile:getUnit() == nil and b:getOwnerObject() == currentPlayerTurn then
    if b:getType() == "BarracksCavalry-player1" or
      b:getType() == "BarracksCavalry-player2" or
      b:getType() == "BarracksMelee-player1" or
      b:getType() == "BarracksMelee-player2" or
      b:getType() == "BarracksRange-player1" or
      b:getType() == "BarracksRange-player2"
      then
      
      local selection = MOAIGfxQuad2D.new()
      selection:setRect(tileProp:getRect())
      
      if currentPlayerTurn == pl1 then
        selection:setTexture(textureSelectionRed)
      else
        selection:setTexture(textureSelectionBlue)
      end
      
      buildingSelectionProp = MOAIProp2D.new()
      buildingSelectionProp:setPriority(51)
      buildingSelectionProp:setDeck(selection)
      buildingSelectionProp:setBlendMode(MOAIProp.GL_SRC_ALPHA, MOAIProp.GL_ONE_MINUS_SRC_ALPHA)
      MainLayer:insertProp(buildingSelectionProp)
      
      if isProductionOverlayShowed == false then
        isProductionOverlayShowed = true
        if propOverlay ~= nil then
          MainLayer:removeProp(propOverlay)
          propOverlay = nil
        end
        
        local widthOverlayX1 = (baseScreenWidth / 2 + camMapX) - 100
        local widthOverlayX2 = (baseScreenWidth / 2 + camMapX) + 100
        local heightOverLayY1 = (baseScreenHeight + camMapY)
        local heightOverLayY2 = (baseScreenHeight + camMapY) + 80

        local spriteOverlay = MOAIGfxQuad2D.new()
        
        if b:getTypeBarracks() == "Range" then
          spriteOverlay:setTexture(textureUnitProductionRange)
        elseif b:getTypeBarracks() == "Melee" then
          spriteOverlay:setTexture(textureUnitProductionMelee)
        elseif b:getTypeBarracks() == "Cavalry" then
          spriteOverlay:setTexture(textureUnitProductionCavalry)  
        end
        
        spriteOverlay:setRect(widthOverlayX1, -heightOverLayY2, widthOverlayX2, -heightOverLayY1)
        
        propOverlay = MOAIProp2D.new()
        propOverlay:setPriority(100)
        propOverlay:setDeck(spriteOverlay)
        propOverlay:setBlendMode(MOAIProp.GL_SRC_ALPHA, MOAIProp.GL_ONE_MINUS_SRC_ALPHA)
        
        MainLayer:insertProp(propOverlay)
        propOverlay:moveLoc(0, 80, 1)
        
        clickedProductionBuilding = buildingsList
      end
    elseif buildingSelectionProp ~= nil then
      MainLayer:removeProp(buildingSelectionProp)
      buildingSelectionProp = nil
    end
  end
end

 return BuildingHelper